package cn.yj.core.business.controller;


import cn.yj.cgs.dba.entity.Comment;
import cn.yj.cgs.dba.entity.vo.CommentVo;
import cn.yj.syslog.common.LogType;
import cn.yj.syslog.common.OperateLog;
import cn.yj.config.annotation.Anonymous;
import cn.yj.config.web.AppBaseController;
import cn.yj.core.business.service.iservice.ICommentService;
import cn.yj.entity.R;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 永健
 * @since 2019-02-23
 */
@RestController
@RequestMapping("/comment")
public class ArticleCommentController extends AppBaseController<CommentVo> {
    @Resource
    ICommentService thisService;

    @GetMapping("/list")
    @Anonymous
    public R list(@RequestParam Map<String, Object> map) {
        map.put("userCode", getUserCode());
        return success(thisService.listPage(map, page()));
    }

    @GetMapping("/listSon")
    @Anonymous
    public R listSon(@RequestParam String articleNo, @RequestParam Integer parentId) {
        return success(thisService.findSonByArticleNoAndPId(articleNo, parentId, getUserCode()));
    }

    @Anonymous
    @GetMapping("/getAllCountNum/{articleNo}")
    public R getAllCountNum(@PathVariable String articleNo){
        return success(thisService.getAllCountNum(articleNo));
    }


    /**
     * <br>
     * 回复/评论
     *
     * @param comment articleNo: 在那篇文章回复
     *                commentId:给哪条评论回复
     *                content: 回复内容
     */
    @OperateLog(describe = "评论回复",type = LogType.UPDATE)
    @PostMapping("/insertSave/reply")
    public R reply(@RequestBody Comment comment) {
        // Comment comment = new Comment();
        // 最顶层的id 在哪条评论下延伸的
        // comment.setParentId(parentId);
        // 直接父Id 在这条下回复
        // comment.setDirectParentId(commentId);
        // 回复于谁
        // comment.setReplyUserId(replyUserId);
        // 谁去评论的
        comment.setUserCode(getUserCode());
        comment.setCreateTime(new Date());
        // 评论内容
        // comment.setContent(content);
        return result(thisService.reply(comment));
    }


    /**
     * <br/>
     * <p>
     * goodCount
     *
     * @return  R
     */
    @OperateLog(describe = "点赞评论",type = LogType.UPDATE)
    @PutMapping("/support/{commentNo}")
    public R support(@PathVariable String commentNo) {
        return result(thisService.agreeComment(commentNo, getUserCode()));
    }

    @OperateLog(describe = "删除评论",type = LogType.DELETE)
    @DeleteMapping("/delete/{commentNo}")
    public R delete(@PathVariable String commentNo) {
        return result(thisService.deleteByNo(commentNo, getUserCode()));
    }

}

